<template>
  <div class="jp-wrapper" :class="{ 'jp-sidebar--fold': sidebarFold }">
    <template>
      <main-navbar ref="navbar" @showRight="showRight" />
      <main-sidebar />
      <div class="jp-content__wrapper">
        <main-content />
      </div>
      <main-right ref="mainRight" />
    </template>

    <!-- <el-dialog
      title="您有新的订单, 请及时处理！"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>订单号：{{msg}}</span>
      <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
  </span>
</el-dialog> -->
  </div>
</template>

<script>
import MainNavbar from "./layout/_common_top";
import MainSidebar from "./layout/_common_left";
import MainContent from "./layout/_common_center";
import MainRight from "./layout/_common_right";
export default {
  data() {
    return {
      socket: null,
      uid:'',
      msg:'',
      dialogVisible: false,
      toUserId: "2",
      content: "3",
      isRightVisible: false,
    };
  },
  components: {
    MainNavbar,
    MainSidebar,
    MainContent,
    MainRight,
  },
  computed: {
    sidebarFold: {
      get() {
        return this.$store.state.common.sidebarFold;
      },
    },
  },
  created() {
  },
  mounted() {
    this.getUserInfo();
    this.getConfig();
    this.resetDocumentClientHeight();
    // this.openSocket();
    // window.setInterval(() => {
    //     setTimeout(this.getNewMessage(), 0);
    //     }, 600000);
  },
  methods: {
    // 重置窗口可视高度
    resetDocumentClientHeight() {
      window.onresize = () => {
        if (this.$refs.navbar) {
          let _defaultLayout = this.$refs.navbar.defaultLayout;
          if (_defaultLayout === "top") {
            this.$refs.navbar.fixTopMenu();
          }
        }
      };
    },
  // 实现轮询
    // 请求是否有新消息
    // getNewMessage: function() {
    //   this.openSocket();
    // },
    showRight(flag) {
      this.$refs.mainRight.showRight();
      this.isRightVisible = flag;
    },
    // 获取当前登录用户信息
    getUserInfo() {
      this.$http({
        url: "/sys/user/info",
        method: "get",
      }).then(({ data }) => {
        if (data.success) {
          this.$store.commit("user/updateUser", data.user);
        }
      });
    },
    // 获取产品name 和 logo
    getConfig() {
      this.$http.get("/sys/sysConfig/getConfig").then(({ data }) => {
        if (data.success) {
          this.$store.commit(
            "config/updateProductName",
            data.config.productName
          );
          this.$store.commit("config/updateLogo", data.config.logo);
          if (!localStorage.getItem("defaultLayout")) {
            this.$store.commit(
              "config/updateDefaultLayout",
              data.config.defaultLayout
            );
          }
          if (!localStorage.getItem("defaultTheme")) {
            this.$store.commit(
              "config/updateDefaultTheme",
              data.config.defaultTheme
            );
          }
        }
      });
    },
    handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
    // openSocket() {
    //   if (typeof WebSocket == "undefined") {
    //     console.log("您的浏览器不支持WebSocket");
    //   } else {
    //     console.log("您的浏览器支持WebSocket");
    //     this.uid = localStorage.getItem('uid');
    //     if (this.uid == '' || this.uid == undefined) {
    //       let name = localStorage.getItem('name');
    //       this.$http({
    //         url: `/sys/user/getUser?name=` + name,
    //         method: 'get'
    //       }).then(({data}) => {
    //         this.uid = data.uid
    //       })
    //     }
        // if (this.uid == '' || this.uid == undefined) {
            
        // }
        //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
        //等同于socket = new WebSocket("ws://localhost:8888/xxxx/im/25");
        //var socketUrl="${request.contextPath}/im/"+$("#userId").val();
    //     var socketUrl = "https://n.shoufu668.com/imserver/" + this.uid;
    //     socketUrl = socketUrl.replace("https", "wss").replace("http", "ws");
    //     if (this.socket != null) {
    //       this.socket.close();
    //       this.socket = null;
    //     }
    //     this.socket = new WebSocket(socketUrl);
    //     //打开事件
    //     this.socket = new WebSocket(socketUrl);
    //     //打开事件
    //     this.socket.onopen = function () {
    //       console.log("websocket已打开");
    //       //socket.send("这是来自客户端的消息" + location.href + new Date());
    //     };
    //     //获得消息事件
    //     this.socket.onmessage = (msg)=> {
    //       this.msg = msg.data
    //       console.log(this.msg);
    //       if (this.msg != '' && this.msg != '连接成功') {
    //           this.dialogVisible = true
    //       }
    //     };
    //     //关闭事件
    //     this.socket.onclose = function () {
    //       console.log("websocket已关闭");
    //     };
    //     //发生了错误事件
    //     this.socket.onerror = function () {
    //       console.log("websocket发生了错误");
    //     };
    //   }
    // },
    // sendMessage() {
    //   if (typeof WebSocket == "undefined") {
    //     console.log("您的浏览器不支持WebSocket");
    //   } else {
    //     console.log("您的浏览器支持WebSocket");
    //   }
    // },
  }
};
</script>